package com.hdax.dm.service.Impl;

import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.beans.order.order;
import com.hdax.dm.dto.queryorderlistDto;
import com.hdax.dm.mappers.queryorderlistMapper;
import com.hdax.dm.mapstucts.queryorderlistDtoMapper;
import com.hdax.dm.service.queryorderlistService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class queryorderlistServiceImpl extends ServiceImpl<queryorderlistMapper,order> implements queryorderlistService {

    @Autowired
    private queryorderlistDtoMapper queryorderlistDtoMapper;

    @Override
    public CommonResponse<List<queryorderlistDto>> queryorderlist(Map<String,Object> params,Integer id) {

        String keyword=(String) params.get("keyword");
        Integer orderTime=(Integer) params.get("orderTime");

        QueryWrapper<order> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("userId",id);
        if (params.get("orderType")!="") {
            Integer orderType= (Integer) params.get("orderType");
            queryWrapper.eq("orderType",orderType);
        }

        if (keyword!="") {
            queryWrapper.like("orderNo",keyword);
        }

        LocalDateTime now = LocalDateTime.now();
        if (orderTime==1) {
            now=now.minus(1, ChronoUnit.YEARS);
            queryWrapper.between("createdTime",now,LocalDateTime.now());
        }else if (orderTime == 2){
            now=now.minus(3, ChronoUnit.MONTHS);
            queryWrapper.between("createdTime",now,LocalDateTime.now());
        }

        List<order> list = baseMapper.selectList(queryWrapper);
        List<queryorderlistDto> dtos = queryorderlistDtoMapper.toTargetList(list);
        return ResponseUtil.returnSuccess(dtos);
    }

    @Override
    public Integer addorder(order order) {
        return baseMapper.insert(order);
    }

    @Override
    public order pay(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("orderNo",orderNo);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer Timeoutupdate(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("orderNo",orderNo);
        order order = new order();
        order.setOrderType(-1L);
        return baseMapper.update(order,queryWrapper);
    }

    @Override
    public Integer succeedupdate(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("orderNo",orderNo);
        order order = new order();
        order.setOrderType(2L);
        return baseMapper.update(order,queryWrapper);
    }
}
